package com.example.blog_system_ssm.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private RefreshTokenInterceptor refreshTokenInterceptor;

    List<String> excludes= new ArrayList<String>(){{
        //放行静态资源
        add("/**/*.html");
        add("/js/**");
        add("/editor.md/**");
        add("/img/**");
        add("/css/**");
        //放行相关接口
        add("/user/reg");
        add("/user/login");
        add("/art/detail");//未登录也允许查看文章详情
        add("/art/list"); // 放行文章分页列表接口
        add("/art/totalpage"); // 放行文章分页总页数接口
        add("/admin/defaultKaptcha");//生成验证码
        add("/user/myinfobyuid");//文章作者信息
        add("/art/myartcountbyuid");//作者文章计数
    }};

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        InterceptorRegistration refreshInterceptorRegistration=registry.addInterceptor(refreshTokenInterceptor).
                addPathPatterns("/**").order(0);

        InterceptorRegistration registration=registry.addInterceptor(loginInterceptor);
        registration.addPathPatterns("/**");
        //设置优先级
        registration.excludePathPatterns(excludes).order(1);
    }
}
